<?php 

	include("config.php");
    include(INCLUDEDIR."core.php");
	include("acceso.php");
	
	$tpl = new Plantilla();
	$tpl->setPlantilla("tpl.academias.html");
	$tpl->prepare();

	$texto = isset($_GET['texto'])?sanitizar($_GET['texto']):"";
	$aprobado = isset($_GET['aprobado'])?intval($_GET['aprobado']):0;
	
	$opciones_aprobado = array( 1 => "Sin Aprobar",
								2 => "Aprobado" );
								
					
	
	if($_GET['idi'] && $_GET['accion']=="eliminar_academia") {
		$id_academia = intval($_GET['idi']); 
		mysql_query("DELETE FROM academias WHERE id_academia = '$id_academia'");
		registrar_log("Ha eliminado ".$academia['nombre_academia']);
		redir($_SERVER['PHP_SELF'],"El academia ha sido eliminado.");
	}

	if($_GET['idi'] && $_GET['accion']=="aprobar_academia") {
		$id_academia = intval($_GET['idi']); 
		mysql_query("UPDATE academias SET aprobado = '1' WHERE id_academia = '$id_academia'");
		redir($_SERVER['HTTP_REFERER'],"Academia Aprobada",false);
	}


	$tpl->newBlock("FORMULARIOACADEMIA");
	$tpl->assign("texto",$texto);
	$tpl->assign("comboaprobados",genCombo("aprobado",$opciones_aprobado,$aprobado,"Todas las Academias"));
	Post2Get();
	
		
	$query = "SELECT __CAMPOS__ FROM academias __WHERE__ ";					
	$orden = "ORDER BY nombre_academia ASC ";
			
		
	$filtros = array();
	if(strlen($texto)>1) {
		$filtros[] = db::SearchText($texto,array ("academias") );
	}
	
	if($aprobado==1) $filtros[] = "academias.aprobado = '0'";	
	if($aprobado==2) $filtros[] = "academias.aprobado = '1'";	
	
	if(count($filtros)>0) {
		$query = str_replace("__WHERE__","WHERE (".implode(" AND ",$filtros).")",$query);	
	} else {
		$query = str_replace("__WHERE__","",$query);	
	}
	
	
	
	// PAGINACION 
	$maxitems = 30;	
	$totalitems =intval(db::ExecuteScalar(str_replace("__CAMPOS__","count(*)",$query)));
	$totalpaginas = ceil($totalitems/$maxitems);
	$pagina_actual = intval($_GET['pa']);
	$offset = $pagina_actual*$maxitems;
	$tpl->assign("paginacion",paginacion($totalpaginas,$pagina_actual,$totalitems));
	// PAGINACION
	
	$consulta = str_replace("__CAMPOS__","*",$query).$orden." LIMIT $maxitems OFFSET $offset";
	$result = mysql_query($consulta);
	if($result && mysql_num_rows($result)>0) {
		$tpl->newBlock("LISTAACADEMIAS");	
		while($fila = mysql_fetch_assoc($result)) {
			$tpl->newBlock("FILAACADEMIA");
			$tpl->assign("es_aprobado",intval($fila['aprobado']));
			$fila['aprobado'] = checked($fila['aprobado']);
			foreach($fila as $key => $value) {
				$tpl->assign($key,$value);	
			}
		}
		
	} else {
		$tpl->newBlock("SINRESULTADOS");	
	}
	
	$tpl->Mostrar();
?>